import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CountServlet extends HttpServlet {
    public void init() throws ServletException{
        int count =0;//给次数设置一个初始值0
        //将0存入到ServletContext中
        ServletContext context= this.getServletContext();
        context.setAttribute("data",count);
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        //将原来的次数count从ServletContext中取出来
        ServletContext context=this.getServletContext();
        Object obj=context.getAttribute("data");
        String s=obj.toString();
        int count= (int)obj;
        count+=1;
        //存入到ServletContext中
        context.setAttribute("data",count);
        //向叶面输入欢迎光临！
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>欢迎光临本网站！这是您第"+count+"次访问</h1>");
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        doGet(request,response);
    }
}